<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :lock-scroll="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="820px"
  >
    <el-form :model="spec" :rules="rules" labelPosition="top" ref="form" class="custom-form">
      <el-form-item label="规格名称：" required>
        <el-row :gutter="20">
          <el-col :span="10">
            <el-input v-model="spec.name" placeholder="输入规格名称"></el-input>
          </el-col>
          <el-col :span="14">
            <span>请填写常用的商品规格的名称；例如：颜色；尺寸等</span>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="显示名称：">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-input v-model="spec.displayName" placeholder="输入显示名称"></el-input>
          </el-col>
          <el-col :span="14">
            <span>展示给用户看的名称；例如：衣服尺寸和裤子的尺寸可以展示名称都是尺寸</span>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="是否显示:" required>
        <el-row :gutter="20">
          <el-col :span="10">
            <el-radio v-model="spec.isShow" :label="1">显示</el-radio>
            <el-radio v-model="spec.isShow" :label="0">隐藏</el-radio>
          </el-col>
          <el-col :span="14">
            <span>请设置规格是否启用</span>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="添加属性:" required>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-tag
              :key="value"
              v-for="value in spec.values"
              closable
              :disable-transitions="false"
              @close="handleClose(value)">
              {{value}}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="inputVisible"
              v-model="inputValue"
              ref="tag"
              size="small"
              @keyup.enter.native="handleInputConfirm"
              @blur="handleInputConfirm"
            >
            </el-input>
            <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 属性</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button type="primary" @click="submitForm"><i class="fa fa-save"></i> 确认保存</el-button>
      <el-button @click="restForm">重置</el-button>
    </div>
  </el-dialog>

</template>

<script>
  import Dialog from 'src/components/common/js/dialog-mix'
  import AdminService from 'src/service/admin'

  export default {
    name: 'AdminCreateSpec',
    mixins: [Dialog],
    data() {
      return {
        spec: {
          name: '',
          displayName: '',
          isShow: 1,
          values: []
        },
        inputVisible: false,
        inputValue: '',
        rules: {}
      }
    },
    props: {
      value: {
        type: Object
      }
    },
    watch: {
      value: function (newValue, oldValue) {
        if (newValue == null) {
          this.spec =
            {
              name: '',
              isShow: 1,
              values: []
            }
        } else {
          this.spec = newValue;
        }
      }
    },
    methods: {
      submitForm: function () {
        const me = this
        this.$refs.form.validate((valid) => {
          if (valid) {
            AdminService.spec(me.spec).then(function (data) {
              if (data != null && data.datas.length > 0) {
                me.$emit('created', data.datas[0])
                me.$message.success('编辑规格成功!')
              }
            })
          } else {
            me.$message.warning('请正确填写信息!')
            return false
          }
        })
      },
      restForm: function () {
        this.$refs.form.resetFields()
      },
      handleClose(tag) {
        this.spec.values.splice(this.spec.values.indexOf(tag), 1)
      },
      showInput() {
        this.inputVisible = true
        this.$nextTick(_ => {
          this.$refs.tag.$refs.input.focus()
        })
      },
      handleInputConfirm() {
        let inputValue = this.inputValue
        if (inputValue) {
          let exist = false
          for (let i = 0; i < this.spec.values.length; i++) {
            if (this.spec.values[i] === inputValue) {
              exist = true
            }
          }
          if (exist) {
            this.$message.warning('改值已经存在了。')
          } else {
            this.spec.values.push(inputValue)
          }
        }
        this.inputVisible = false
        this.inputValue = ''
      }
    },
    created: function () {
      if (this.value == null) {
        this.spec = {
          name: '',
          isShow: 1,
          values: []
        }
      } else {
        this.spec = this.value;
      }
    }
  }
</script>
